package com.example.xd.defaultcall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import static com.example.xd.defaultcall.Constance.RECEIVE_BR_URL;

public class UIActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTv_num;
    private ImageView mImg_ans;
    private ImageView mImg_cut;
    private PhoneCallManager mPhoneCallManager;
    private TextView mTv_time;
    private long time = 0;
    private boolean isUsed = false;
    private ReceivePhoneStateBr mReceivePhoneStateBr;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 2:
                    time++;
                    mTv_time.setText("通话时长:" + TimeUtils.stringForTime(time));
                    Message message = Message.obtain();
                    message.what = 2;
                    mHandler.sendMessageDelayed(message, 1000);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏状态下显示
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕长亮
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); //打开屏幕
        setContentView(R.layout.activity_ui);
        mTv_num = findViewById(R.id.tv_num);
        mImg_ans = findViewById(R.id.img_ans);
        mImg_cut = findViewById(R.id.img_cut);
        mTv_time = findViewById(R.id.tv_time);

        mImg_ans.setOnClickListener(this);
        mImg_cut.setOnClickListener(this);
        mPhoneCallManager = new PhoneCallManager(this);

        MyApp application = (MyApp) getApplication();
        application.setActivity(this);
        String tel = getIntent().getStringExtra("tel");
        mTv_num.setText(tel);

        mReceivePhoneStateBr = new ReceivePhoneStateBr();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RECEIVE_BR_URL);
        registerReceiver(mReceivePhoneStateBr, intentFilter);
        Log.i("xd----", "onCreate");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_ans:
                mPhoneCallManager.answer();
                break;
            case R.id.img_cut:
                if (!isUsed) {
                    mPhoneCallManager.disconnect();
                    mPhoneCallManager.destroy();
                    isUsed = true;
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public class ReceivePhoneStateBr extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String state = intent.getStringExtra("state");
            if (action == RECEIVE_BR_URL) {
                switch (state) {
                    case "STATE_ACTIVE"://来电时,我接通电话
                        mTv_time.setText("来电,我接通电话");
                        Message message = Message.obtain();
                        message.what = 2;
                        mHandler.sendMessage(message);
                        break;
                    case "STATE_DISCONNECTED"://来电时,对方挂断电话
                        if (!isUsed) {
                            mPhoneCallManager.disconnect();
                            mPhoneCallManager.destroy();
                            unregisterReceiver(mReceivePhoneStateBr);

                            isUsed = true;
                        }
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }
                        if (time == 0) {
                            mTv_time.setText("挂断结束");
                        } else {
                            mTv_time.setText("电话结束:" + TimeUtils.stringForTime(time));
                        }
                        break;
                    case "STATE_DISCONNECTING"://来电时,自己挂断电话
                        if (!isUsed) {
                            mPhoneCallManager.disconnect();
                            mPhoneCallManager.destroy();
                            unregisterReceiver(mReceivePhoneStateBr);
                            isUsed = true;
                        }
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }
                        if (time == 0) {
                            mTv_time.setText("正在挂断");
                        } else {
                            mTv_time.setText("电话结束:" + TimeUtils.stringForTime(time));
                        }
                        break;
                }
            }
        }
    }
}
